package com.tngames.angrytunisian.game.core.splingshot;

import static playn.core.PlayN.pointer;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import playn.core.Image;
import playn.core.Pointer;
import playn.core.Pointer.Event;

import com.tngames.angrytunisian.game.core.AngryTunisianGameEngine;
import com.tngames.angrytunisian.game.core.Constants;
import com.tngames.angrytunisian.game.core.Resources;
import com.tngames.angrytunisian.game.core.entity.DynamicPhysicsEntity;
import com.tngames.angrytunisian.game.core.entity.EntityEngine;

public class PeaTest extends DynamicPhysicsEntity implements Pointer.Listener {

	private static final String IMAGE = Resources.PEA_TEST;

	private static final float RADIUS = 37 / 2 * Constants.PHYS_UNIT_PER_SCREEN_UNIT;
	private static final float HEIGHT = 37 * Constants.PHYS_UNIT_PER_SCREEN_UNIT;
	private static final float WIDTH = 37 * Constants.PHYS_UNIT_PER_SCREEN_UNIT;

	private static int speed = 10;

	private Vec2 shotVec;
	private boolean ready;

	public PeaTest(EntityEngine entityEngine, float x, float y) {
		super(entityEngine, x, y, 0);
		pointer().setListener(this);
		this.ready = false;
		this.shotVec = new Vec2();
	}

	@Override
	protected Body initPhysicsBody(World world, Body groundBody, float x,
			float y, float angle) {
		FixtureDef fixtureDef = new FixtureDef();
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.STATIC;
		bodyDef.position = new Vec2(0, 0);
		Body body = world.createBody(bodyDef);

		CircleShape circleShape = new CircleShape();
		circleShape.m_radius = getRadius();
		fixtureDef.shape = circleShape;
		fixtureDef.density = 0.4f;
		fixtureDef.friction = 0.1f;
		fixtureDef.restitution = 0.35f;
		circleShape.m_p.set(0, 0);
		body.createFixture(fixtureDef);
		body.setLinearDamping(0.2f);
		body.setTransform(new Vec2(x, y), angle);
		return body;
	}

	@Override
	public void update(float delta) {
		if (this.ready) {
			super.update(delta);
		}
	}

	@Override
	public float getWidth() {
		return WIDTH;
	}

	@Override
	public float getHeight() {
		return HEIGHT;
	}

	public static float getRadius() {
		return RADIUS;
	}

	@Override
	public void onPointerStart(Event event) {
		this.ready = false;
		getBody().setType(BodyType.STATIC);
	}

	@Override
	public void onPointerEnd(Event event) {
		this.ready = true;
		getBody().setType(BodyType.DYNAMIC);
//		pointer().setListener(null);
//		this.setLinearVelocity(shotVec.x * speed, shotVec.y * speed);
	}

	private void shoot() {

	}

	@Override
	public void onPointerDrag(Event event) {

		float scaledEventX = event.x() * Constants.PHYS_UNIT_PER_SCREEN_UNIT;
		float scaledEventY = event.y() * Constants.PHYS_UNIT_PER_SCREEN_UNIT;
//
//		float diffX = AngryTunisianGameEngine.getSlingShot()
//				.getAbsoluteCenterPosition().x - scaledEventX;
//		float diffY = AngryTunisianGameEngine.getSlingShot()
//				.getAbsoluteCenterPosition().y - scaledEventY;
//		Vec2 vect = new Vec2(diffX, diffY);
//		if (vect.length() > Constants.MAX_SLING_RADIUS) {
//			this.shotVec = vect.mul(Constants.MAX_SLING_RADIUS / vect.length());
//			this.setPos(AngryTunisianGameEngine.getSlingShot()
//					.getAbsoluteCenterPosition().x - shotVec.x,
//					AngryTunisianGameEngine.getSlingShot()
//							.getAbsoluteCenterPosition().y - shotVec.y);
//		} else {
//			// this.shotVec.set(diffX, diffY);
//			this.setPos(scaledEventX, scaledEventY);
//
//		}
		//
		this.setPos(scaledEventX, scaledEventY);
	}

	@Override
	public Image getImage() {
		return loadImage(IMAGE);
	}
}
